
#include "Manga_ui_logic.h"
#include <QDebug>
#include"product_detail_widget.h"
using namespace std;

EventGridWidget::EventGridWidget( QWidget *parent)
    : QWidget(parent), currentPage(0), eventWidgets(new QList<EventDisplayWidget *>())
{
    mainLayout = new QVBoxLayout(this);
    eventListLayout = new QVBoxLayout();
    paginationLayout = new QHBoxLayout();

    QPushButton *prevButton = new QPushButton("上一页", this);
    QPushButton *nextButton = new QPushButton("下一页", this);

    paginationLayout->addWidget(prevButton);
    paginationLayout->addStretch();
    paginationLayout->addWidget(nextButton);

    connect(prevButton, &QPushButton::clicked, this, &EventGridWidget::previousPage);
    connect(nextButton, &QPushButton::clicked, this, &EventGridWidget::nextPage);

    mainLayout->addLayout(eventListLayout);
    mainLayout->addLayout(paginationLayout);

    setLayout(mainLayout);
}

EventGridWidget::~EventGridWidget()
{
    // 清理 eventWidgets 中的所有指针
    qDeleteAll(*eventWidgets);
    delete eventWidgets;
}

void EventGridWidget::populateEventWidgets()
{
    qDeleteAll(*eventWidgets);
    eventWidgets->clear();
    // 仅在第一次填充时创建 eventWidgets
    if ((*eventWidgets).isEmpty()) {
        for (int i = 0; i < mangaProductList.size(); ++i)
        {
            EventDisplayWidget *eventWidget = new EventDisplayWidget(this);
            (*eventWidgets).append(eventWidget);

            //connect(eventWidget,&EventDisplayWidget::buyButtonClicked,this,&EventGridWidget::handleBuyButtonClicked);
            connect(eventWidget, &EventDisplayWidget::productClicked, this, &EventGridWidget::showProductDetail);
        }
    }

    loadPage(currentPage); // 加载当前页
}

void EventGridWidget::setEventData(QPixmap image,
                                   QString theme,
                                   QDate date,
                                   QString location,
                                   QString price,
                                 QString description,
                                   int productId,
                                   int index)
{
    if (index < (*eventWidgets).size())
    {
        EventDisplayWidget *eventWidget = (*eventWidgets)[index];
        eventWidget->setEvent(image, theme, date, location, price, description,productId); // 设置产品数据
    }
}

void EventGridWidget::loadPage(int page)
{
    // Clear the layout and add widgets for the current page
    QLayoutItem *child;
    while ((child = eventListLayout->takeAt(0)) != nullptr)
    {
        // Remove the widget from the layout without deleting it
        child->widget()->setParent(nullptr);
        delete child;  // Delete the layout item, not the widget
    }

    int startIndex = page * itemsPerPage;
    int endIndex = std::min(startIndex + itemsPerPage, static_cast<int>((*eventWidgets).size()));

    for (int i = startIndex; i < endIndex; ++i)
    {
        if ((*eventWidgets)[i] != nullptr) {
            eventListLayout->addWidget((*eventWidgets)[i]);
        } else {
            qDebug() << "Warning: eventWidgets[" << i << "] is a null pointer.";
        }
    }

    updatePagination();
}

void EventGridWidget::updatePagination()
{
    bool hasNextPage = (currentPage + 1) * itemsPerPage < (*eventWidgets).size();
    bool hasPreviousPage = currentPage > 0;

    paginationLayout->itemAt(0)->widget()->setEnabled(hasPreviousPage); // Previous button
    paginationLayout->itemAt(2)->widget()->setEnabled(hasNextPage);     // Next button
}

void EventGridWidget::nextPage()
{
    if ((currentPage + 1) * itemsPerPage < (*eventWidgets).size())
    {
        ++currentPage;
        loadPage(currentPage);
    }
}

void EventGridWidget::previousPage()
{
    qDebug() << "Current page before decrement: " << currentPage;
    if (currentPage > 0)
    {
        --currentPage;
        qDebug() << "Loading previous page: " << currentPage;
        loadPage(currentPage);
    }
}

/*void EventGridWidget::handleBuyButtonClicked(const QString &productName,
                                             const QString &productPrice,
                                             int productId)
{
    qDebug() << "购买商品: " << productName << " 价格: " << productPrice
             << " 商品ID: " << productId;
    shoppingCartLogic->addProductRow(productName, productPrice, productId, 1);
}*/
void EventGridWidget::showProductDetail(const QPixmap &image, const QString &theme,
                                        const QString &description, const QString &location,const QString date,
                                        const QString &price, int productId)
{
    ProductDetailWidget *detailWidget = new ProductDetailWidget();

    // Assuming your ProductDetailWidget has a method to set up with provided information
    detailWidget->setupProductInfo(image, theme, description, location, price,date, productId);

    // Show the ProductDetailWidget (you may want to use a dialog, depending on your design)
    detailWidget->setWindowTitle("Product Details");
    detailWidget->setAttribute(Qt::WA_DeleteOnClose); // Ensure it gets deleted when closed
    detailWidget->show();
}


